package com.exam.controller;

import com.exam.aop.MyLog;
import com.exam.dto.ActionResult;
import com.exam.po.Announcement;
import com.exam.service.AnnounceService;
import com.exam.utils.ApiResultHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhanggaoxiang
 * @ClassName AnnounceController   通告
 * @Description TODO
 * @Date 2021/1/11 19:00
 * @Version 1.0
 **/
@RestController
@Slf4j
@RequestMapping("/api")
public class AnnounceController {
    @Autowired
     private AnnounceService announceService;
    /**
     * @Author zhanggaoxiang
     * @Description 分页查询通知
     * @Date 2021/1/11 19:50
     * @Param []
     * @return com.exam.dto.ActionResult
     **/
    @GetMapping("/findannounce/{page}/{size}")
    @ResponseBody
    @MyLog(name ="find announce")
    public ActionResult findAnnounceByPage(@PathVariable Integer page,@PathVariable Integer size){

        Map<String,Object> map=new HashMap<>();
        Integer total=announceService.findAnnounceCount();
        List<Announcement> list=announceService.findAnnounceByPage(page,size);
        map.put("total",total);
        map.put("data",list);

        if (map!=null) {
            return new ActionResult(200,"分页查询所有通知",map);
        }else {
            return ApiResultHandler.buildActionResult(404, "查询失败!!!", null);
        }
    }
    /**
     * @Author zhanggaoxiang
     * @Description 添加通知
     * @Date 2021/1/12 09:40
     * @Param [announcement]
     * @return com.exam.dto.ActionResult
     **/
    @PostMapping("/addannounce")
    @ResponseBody
    @MyLog(name = "add announce")
    public ActionResult addAnnounce(@RequestBody Announcement announcement){
        int i = announceService.addAnnounce(announcement);
        if (i>0){
            return ApiResultHandler.buildActionResult(200,"添加成功!!",null);
        }else {
            return ApiResultHandler.buildActionResult(403,"添加失败!!",null);
        }

    }
    /**
     * @Author zhanggaoxiang
     * @Description 按照id删除通知
     * @Date 2021/1/12 10:14
     * @Param [announcementid]
     * @return com.exam.dto.ActionResult
     **/
    @DeleteMapping("/deleteannouce/{announcementid}")
    @ResponseBody
    @MyLog(name = "delete announce")
    public ActionResult deleteAnnounceById(@PathVariable Integer announcementid){

        int i=announceService.deleteAnnounceById(announcementid);
        if (i>0){
            return ApiResultHandler.buildActionResult(200,"删除成功!!",null);
        }else {
            return ApiResultHandler.buildActionResult(200,"删除失败!!",null);
        }
    }
}
